from django.contrib.auth.models import User
from django.db import models

from item.models import Item


class Conversation(models.Model):
    item = models.ForeignKey(Item, related_name='conversations',
                             on_delete=models.CASCADE)
    members = models.ManyToManyField(User, related_name='conversations')
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    modified_at = models.DateTimeField("最后修改时间", auto_now=True)

    class Meta:
        ordering = ('-modified_at',)

    def __str__(self):
        return self.item.name


class ConversationMessage(models.Model):
    conversation = models.ForeignKey(Conversation, related_name='messages',
                                     on_delete=models.CASCADE)
    content = models.TextField(verbose_name='内容')
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    created_by = models.ForeignKey(User, related_name='created_messages',
                                   on_delete=models.CASCADE, verbose_name='添加人')